package com.hsbc.dev.teamo4.sms.sm.service.impl;

import com.google.common.base.Preconditions;
import com.google.common.collect.Lists;
import com.hsbc.dev.teamo4.sms.common.utils.data.BeanValidator;
import com.hsbc.dev.teamo4.sms.common.utils.data.LevelUtil;
import com.hsbc.dev.teamo4.sms.common.utils.factory.UUIDFactory;
import com.hsbc.dev.teamo4.sms.sm.dao.IDepartmentDao;
import com.hsbc.dev.teamo4.sms.sm.dao.impl.DepartmentDaoImpl;
import com.hsbc.dev.teamo4.sms.sm.po.Department;
import com.hsbc.dev.teamo4.sms.sm.service.IDepartmentService;
import org.springframework.util.CollectionUtils;

import java.util.Collections;
import java.util.List;
import java.util.Map;

/**
 * @author:Kevin
 * @version:
 * @Project: permission_manage
 * @Package: com.hsbc.dev.teamo4.sms.sm.service.impl
 * @Description:
 * @Date date: 2018/7/28
 */
public class DepartmentServiceImpl implements IDepartmentService{
    private IDepartmentDao iDepartmentDao=new DepartmentDaoImpl();
    @Override
    public Department save(Department department) {
        BeanValidator.validateObject(department);
        if(checkExist(department.getParentId(),department.getDepartmentName(),department.getId())){
            try {
                throw new Exception("the same level have same department");
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        department.setDepartmentLevel(LevelUtil.calculateLevel(getLevel(department.getParentId()),department.getDepartmentOrder()));
        String id=UUIDFactory.getUUID();
        department.setId(id);
        department.setCreateId(id);
        department.setCreateTime(String.valueOf(System.currentTimeMillis()));
        department.setUpdateId(id);
        department.setCreateTime(String.valueOf(System.currentTimeMillis()));
        department.setIsEnable(true);
        return iDepartmentDao.saveEntity(department);
    }

    @Override
    public boolean checkExist(String parentId, String departmentName, String departmentId){
        Map map=iDepartmentDao.countByNameAndParentId(parentId,departmentName,departmentId);
        return map != null;

    }

    private String getLevel(String departmentId){
        Department department=queryById(departmentId);
        if(department==null){
            return null;
        }
        return department.getDepartmentLevel();
    }

    @Override
    public Department update(Department department) {
        BeanValidator.validateObject(department);
        if(checkExist(department.getParentId(),department.getDepartmentName(),department.getId())){
            try {
                throw new Exception("the same level have same department");
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        Department beforeDepartment=iDepartmentDao.queryById(department.getId());
        Preconditions.checkNotNull(beforeDepartment,"update department is not exist");

        if(checkExist(department.getParentId(),department.getDepartmentName(),department.getId())){
            try {
                throw new Exception("the same level have same department");
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        department.setDepartmentLevel(LevelUtil.calculateLevel(getLevel(department.getParentId()),department.getDepartmentOrder()));
        department.setUpdateTime(String.valueOf(System.currentTimeMillis()));
        updateWithChild(beforeDepartment,department);
        return department;
    }

    /**
     * updateWithChild
     * @param beforeDepartment
     * @param afterDepartment
     */
    private void updateWithChild(Department beforeDepartment,Department afterDepartment){
        String newLevelPrefix=afterDepartment.getDepartmentLevel();
        String oldLevelPrefix=beforeDepartment.getDepartmentLevel();
        if(!afterDepartment.getDepartmentLevel().equals(beforeDepartment.getDepartmentLevel())){
            List<Department> departmentList=iDepartmentDao.getChildDepartmentListByLevel(beforeDepartment.getDepartmentLevel());
                if(CollectionUtils.isEmpty(departmentList)){
                    for (Department department:departmentList){
                        String level=department.getDepartmentLevel();
                        if(level.indexOf(oldLevelPrefix)==0){
                            level=newLevelPrefix+level.substring(oldLevelPrefix.length());
                            department.setDepartmentLevel(level);
                        }
                    }
                    iDepartmentDao.batchUpdateLevel(departmentList);
                }
        }
        iDepartmentDao.updateEntity(afterDepartment);
    }

    @Override
    public Department queryById(String id) {
        return iDepartmentDao.queryById(id);
    }

    @Override
    public List<Department> queryEntityList() {
        return iDepartmentDao.queryAllEntity();
    }

    @Override
    public int updateByIsEnable(String id, Boolean bool) {
        return iDepartmentDao.updateByIsEnable(id,bool);
    }
}
